from generator.proto_generator.proto_map_writer import ProtoMapWriter
from generator.proto_generator.proto_list_writer import ProtoListWriter
from generator.proto_generator.proto_list_map_writer import ProtoListMapWriter

class ProtoTableArrayWriter(object):
	def __init__(self, file_name, table_desc_array):
		self.file_name = file_name
		self.table_desc_array = table_desc_array

	def write(self):
		with open(self.file_name, 'w') as f:
			self.write_class_head(f)
			self.write_class_body(f)
			f.flush()

	def write_class_head(self, f):
		f.write('//This file is generated by program. DO NOT EDIT IT MANUALLY!\n')
		f.write('option optimize_for = LITE_RUNTIME;\n\n')

	def write_class_body(self, f):
		for table_desc in self.table_desc_array:
			table_type = table_desc['table_type']
			if table_type == 'map':
				proto_map_writer = ProtoMapWriter(table_desc, f)
				proto_map_writer.write()
			elif table_type == 'list':
				proto_list_writer = ProtoListWriter(table_desc, f)
				proto_list_writer.write()
			elif table_type == 'list_map':
				proto_list_map_writer = ProtoListMapWriter(table_desc, f)
				proto_list_map_writer.write()